package net.siufung.market.base.log.operate.event;

import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.SubscriberExceptionContext;
import com.google.common.eventbus.SubscriberExceptionHandler;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.siufung.core.event.AbstractEventBus;

import javax.annotation.Nonnull;

/**
 * @author 陈建峰
 * @date 2023/2/15 10:06 AM
 * @since V1.0.0
 */
@Slf4j
@AllArgsConstructor
public class OperateLogAsyncEventBus extends AbstractEventBus implements SubscriberExceptionHandler {

    private final AsyncEventBus asyncEventBus;

    @Override
    public void handleException(Throwable throwable, @Nonnull SubscriberExceptionContext subscriberExceptionContext) {
        // 捕获信息后抛异常或者记录下来
        log.error("demo event handler exception, {}", throwable.getMessage());
    }

    @Override
    public void post(Object event) {
        asyncEventBus.post(event);
    }

    @Override
    public void addConsumer(Object obj) {
        asyncEventBus.register(obj);
    }

    @Override
    public void removeConsumer(Object obj) {
        asyncEventBus.unregister(obj);
    }
}
